// 环境配置
interface EnvConfig {
  // 基础配置
  NODE_ENV: string;
  VITE_APP_TITLE: string;
  VITE_APP_VERSION: string;
  VITE_APP_NAME: string;

  // 登录页面配置（Bigan 特有）
  VITE_LOGIN_WELCOME_TEXT?: string;
  VITE_LOGIN_SUBTITLE_TEXT?: string;

  // 调试配置
  VITE_DELETE_CONSOLE: string;
  VITE_SHOW_SOURCEMAP: string;

  // API配置
  VITE_API_BASE_URL: string;
  VITE_MOCK_URL: string;

  // 其他配置
  VITE_UPLOAD_URL: string;
  VITE_WEBSOCKET_URL: string;

  // 环境特定配置
  VITE_DEBUG_MODE?: string;
  VITE_LOG_LEVEL?: string;

  // 图片和AI相关配置
  VITE_IMAGE_BASE_URL: string;
  VITE_OPEN_MOKE: string;

  // 业务配置
  VITE_TENANT_ID: string;
  VITE_BIZ_ROLE_APP_ID: string;
  VITE_APP_ID: string;

  // 项目特定配置
  VITE_OPEN_SUB_MINI?: string; // Bigan 特有
}

// 环境配置对象
export const envConfig = {
  // 基础配置
  NODE_ENV: (import.meta.env?.NODE_ENV || process.env?.NODE_ENV || 'development') as string,
  APP_TITLE: (import.meta.env?.VITE_APP_TITLE || process.env?.VITE_APP_TITLE || 'BSIN Scaffold App') as string,
  APP_VERSION: (import.meta.env?.VITE_APP_VERSION || process.env?.VITE_APP_VERSION || '1.0.0') as string,
  APP_NAME: (import.meta.env?.VITE_APP_NAME || process.env?.VITE_APP_NAME || 'scaffold') as string,

  // 登录页面配置（Bigan 特有，其他项目可选）
  LOGIN_WELCOME_TEXT: (import.meta.env?.VITE_LOGIN_WELCOME_TEXT ||
    process.env?.VITE_LOGIN_WELCOME_TEXT ||
    '') as string,
  LOGIN_SUBTITLE_TEXT: (import.meta.env?.VITE_LOGIN_SUBTITLE_TEXT ||
    process.env?.VITE_LOGIN_SUBTITLE_TEXT ||
    '') as string,

  // 调试配置
  DELETE_CONSOLE: (import.meta.env?.VITE_DELETE_CONSOLE || process.env?.VITE_DELETE_CONSOLE) === 'true',
  SHOW_SOURCEMAP: (import.meta.env?.VITE_SHOW_SOURCEMAP || process.env?.VITE_SHOW_SOURCEMAP) === 'true',

  // API配置 - 优先使用环境变量，如果没有则使用默认值
  API_BASE_URL: (import.meta.env?.VITE_API_BASE_URL || process.env?.VITE_API_BASE_URL || '') as string,
  MOCK_URL: (import.meta.env?.VITE_MOCK_URL || process.env?.VITE_MOCK_URL || 'http://localhost:3000') as string,

  // 其他配置
  UPLOAD_URL: (import.meta.env?.VITE_UPLOAD_URL || process.env?.VITE_UPLOAD_URL || '') as string,
  WEBSOCKET_URL: (import.meta.env?.VITE_WEBSOCKET_URL || process.env?.VITE_WEBSOCKET_URL || '') as string,

  // 环境特定配置
  DEBUG_MODE: (import.meta.env?.VITE_DEBUG_MODE || process.env?.VITE_DEBUG_MODE) === 'true',
  LOG_LEVEL: (import.meta.env?.VITE_LOG_LEVEL || process.env?.VITE_LOG_LEVEL || 'info') as string,

  // 项目特定配置（可选）
  OPEN_SUB_MINI: (import.meta.env?.VITE_OPEN_SUB_MINI || process.env?.VITE_OPEN_SUB_MINI) === 'true',

  // 业务相关配置
  TENANT_ID: (import.meta.env?.VITE_TENANT_ID || process.env?.VITE_TENANT_ID || '') as string,
  BIZ_ROLE_APP_ID: (import.meta.env?.VITE_BIZ_ROLE_APP_ID || process.env?.VITE_BIZ_ROLE_APP_ID || '') as string,
  APP_ID: (import.meta.env?.VITE_APP_ID || process.env?.VITE_APP_ID || '') as string,

  // 图片和AI相关配置
  IMAGE_BASE_URL: (import.meta.env?.VITE_IMAGE_BASE_URL || process.env?.VITE_IMAGE_BASE_URL || '') as string,
  OPEN_MOKE: (import.meta.env?.VITE_OPEN_MOKE || process.env?.VITE_OPEN_MOKE) === 'true'
};

// 获取当前环境
export const currentEnv = envConfig.NODE_ENV;

// 判断是否为开发环境
export const isDevelopment = currentEnv === 'development';

// 判断是否为生产环境
export const isProduction = currentEnv === 'production';

// 判断是否为测试环境
export const isTest = currentEnv === 'test';

// 获取基础URL - 统一逻辑
export const baseUrl: string = envConfig.OPEN_MOKE ? envConfig.MOCK_URL : envConfig.API_BASE_URL;

// 兼容旧版本的导出
export const mockUrl = envConfig.MOCK_URL;

// 导出配置对象
export default envConfig;
